package icu.qx.qc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.qc.domain.QcQualityProject;
import icu.qx.qc.domain.param.QcOutgoingQualityParam;
import icu.qx.qc.feign.GbsGoodService;
import icu.qx.qc.feign.SysUserFeign;
import icu.qx.qc.service.IQcQualityQprojectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.qc.domain.QcOutgoingQuality;
import icu.qx.qc.service.IQcOutgoingQualityService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 出货检验单Controller
 *
 * @author yong
 * @date 2024-06-25
 */
@RestController
@RequestMapping("/qc/outgoingQuality")
public class QcOutgoingQualityController extends BaseController {
    @Autowired
    private IQcOutgoingQualityService qcOutgoingQualityService;
    @Autowired
    private SysUserFeign sysUserFeign;
    @Autowired
    private IQcQualityQprojectService qcQualityQprojectService;
    @Autowired
    private GbsGoodService gbsGoodService;
    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询出货检验单列表
     */
    @RequiresPermissions("qc:outgoingQuality:list")
    @GetMapping("/list")
    public TableDataInfo list(QcOutgoingQuality qcOutgoingQuality) {
        startPage();
        List<QcOutgoingQuality> list = qcOutgoingQualityService.selectQcOutgoingQualityList(qcOutgoingQuality);
        return getDataTable(list);
    }

    /**
     * 导出出货检验单列表
     */
    @RequiresPermissions("qc:outgoingQuality:export")
    @Log(title = "出货检验单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcOutgoingQuality qcOutgoingQuality) {
        List<QcOutgoingQuality> list = qcOutgoingQualityService.selectQcOutgoingQualityList(qcOutgoingQuality);
        ExcelUtil<QcOutgoingQuality> util = new ExcelUtil<QcOutgoingQuality>(QcOutgoingQuality.class);
        util.exportExcel(response, list, "出货检验单数据");
    }

    /**
     * 获取出货检验单详细信息
     */
    @RequiresPermissions("qc:outgoingQuality:query")
    @GetMapping(value = "/{outQuaCode}")
    public AjaxResult getInfo(@PathVariable("outQuaCode") String outQuaCode) {
        QcOutgoingQuality qcOutgoingQuality = qcOutgoingQualityService.selectQcOutgoingQualityByOutQuaCode(outQuaCode);
        AjaxResult ajaxResult = success(qcOutgoingQuality);
        AjaxResult r1 = sysUserFeign.getInfo(qcOutgoingQuality.getOutQuaPeo());
        AjaxResult r2 = gbsGoodService.getInfo(qcOutgoingQuality.getOutQuaProduct());
        List<QcQualityProject> qcQualityProjects = qcQualityQprojectService.selectQcQualityQprojectByQqProCheckCode(outQuaCode);
        ajaxResult.put("selectedQcPeo", r1.get("data"));
//        ajaxResult.put("selectedGood", r2.get("data"));
        ajaxResult.put("selectedProjectList", qcQualityProjects);
        return ajaxResult;
    }

    /**
     * 新增出货检验单
     */
    @RequiresPermissions("qc:outgoingQuality:add")
    @Log(title = "出货检验单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QcOutgoingQualityParam qcOutgoingQualityParam) {
        QcOutgoingQuality qcOutgoingQuality = new QcOutgoingQuality();
        BeanUtils.copyProperties(qcOutgoingQualityParam, qcOutgoingQuality);

        String code = codeUtils.generateCode(TableNameEnum.QC_OUTGOING_QUALITY.getTableName());
        qcOutgoingQuality.setOutQuaCode(code);

        return toAjax(qcOutgoingQualityService.insertQcOutgoingQuality(qcOutgoingQuality, qcOutgoingQualityParam.getSelectedProjectList()));
    }

    /**
     * 修改出货检验单
     */
    @RequiresPermissions("qc:outgoingQuality:edit")
    @Log(title = "出货检验单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QcOutgoingQualityParam qcOutgoingQualityParam) {
        QcOutgoingQuality qcOutgoingQuality = new QcOutgoingQuality();
        BeanUtils.copyProperties(qcOutgoingQualityParam, qcOutgoingQuality);

        return toAjax(qcOutgoingQualityService.updateQcOutgoingQuality(qcOutgoingQuality, qcOutgoingQualityParam.getSelectedProjectList()));
    }

    /**
     * 删除出货检验单
     */
    @RequiresPermissions("qc:outgoingQuality:remove")
    @Log(title = "出货检验单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{outQuaCodes}")
    public AjaxResult remove(@PathVariable String[] outQuaCodes) {
        return toAjax(qcOutgoingQualityService.deleteQcOutgoingQualityByOutQuaCodes(outQuaCodes));
    }
}
